/*
 * sockets.h
 *
 *  Created on: May 19, 2012
 *      Author: burek
 */

#ifndef SOCKETS_H_
#define SOCKETS_H_

#include "common-codes.h"

#ifdef WIN32
#include <winsock2.h>
#else
#include <netinet/in.h>
#endif

#define MAX_PENDING (32)

#define CU_TCP (1)
#define CU_UDP (2)

// Multiplatformnost i prakticnosti radi, socket je
// enkapsuliran u posebnu strukturu, sa dodatnim podacima
typedef struct
{
	int nix_socketDesc;
	int win_socket;
	unsigned int ipAddr;
	char ipTxt[16];
	int port;
	int protocol;
	struct sockaddr_in hostAddr;
} CUSocket;

CUSocket * cu_createTCPSocket();
CUSocket * cu_createUDPSocket();

int cu_openServerSocket(CUSocket * sock, int port);
int cu_openClientSocket(CUSocket * sock, const char * address, int port);
CUSocket * cu_acceptConnection(CUSocket * sock);
int cu_closeSocket(CUSocket * sock);

int cu_sendData(CUSocket * sock, const char * buffer, int length);
int cu_recvData(CUSocket * sock, char * buffer, int length);
int cu_recvSpin(CUSocket * sock, char * buffer, int length, int cycle, int count);

int cu_sendDataToIP(CUSocket * sock, const char * buffer, int length, unsigned int ip, int port);
int cu_sendDataTo(CUSocket * sock, const char * buffer, int length, const char * ip, int port);
int cu_recvDataFrom(CUSocket * sock, char * buffer, int length, unsigned int * ip, char * ipTxt, int * port);

int cu_checkIPaddress(const char * address);
const char * cu_getSocketError();
void cu_clearSocketError();

unsigned int cu_resolveDomain(const char * dname);
int cu_resolveDomainToTxt(const char *dname, char *iptxt);
void cu_IPtoText(unsigned int ip, char * txt);
unsigned int cu_textToIP(const char * txt);

#endif /* SOCKETS_H_ */
